package com.sms.controller;

import cn.hutool.json.JSONUtil;
import com.sms.common.A;
import com.sms.pojo.Users;
import com.sms.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// 用户注册
@WebServlet("/UsersRegister")
public class UsersRegisterController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Users user = new Users();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        String repassword = request.getParameter("repassword");

        UsersServiceImpl service = new UsersServiceImpl();
        service.save(user);
        service.getSqlSession().close();
        String username = user.getUsername();
        String password = user.getPassword();

        if (username == null || username.isEmpty()) {
            response.getWriter().write(JSONUtil.toJsonStr(A.fail("用户名不能为空")));
        }

        if (password == null || password.isEmpty()) {
            response.getWriter().write(JSONUtil.toJsonStr(A.fail("密码不能为空")));
        }

        if (!password.equals(repassword)) {
            response.getWriter().write(JSONUtil.toJsonStr(A.fail("密码与确认密码不一致")));
        }

        response.getWriter().write(JSONUtil.toJsonStr(A.success("注册成功")));

    }
}
